﻿Imports System.Data.SqlClient
Public Class FormProductEditor
 
    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

        'validate other textbox...........

        If Me.txtPrice.Text = "" Then
            MsgBox("Please input product price!")
            Me.txtPrice.Focus()
            Return
        End If


        If Validator.IsDecimal(txtPrice.Text) = False Then
            MsgBox("Price must be a decimal value!")
            Me.txtPrice.Focus()
            Me.txtPrice.SelectAll()
            Exit Sub
        End If

        If Convert.ToDecimal(Me.txtPrice.Text) <= 0 Then
            MsgBox("Price must be positive")
            Me.txtPrice.Focus()
            Me.txtPrice.SelectAll()
            Exit Sub
        End If
        Try

      
        Cmd.Parameters("@ProductCode") = Me.txtProductCode.Text
        Cmd.Parameters("@ProductName") = Me.txtProductName.Text
            Cmd.Parameters("@CategoryId") = Me.cboCategory.SelectedValue
            Cmd.Parameters("@CateId") = Me.CboCateId.Text
        Cmd.Parameters("@Price") = Me.txtPrice.Text
        Cmd.Parameters("@MinimumPrice") = Me.txtMinimumPrice.Text
            If PID.Text = "" Then
                Cmd.ExecuteNonQuery("insert into Product values (@ProductCode,@ProductName,@CategoryId,@Price,@MinimumPrice,1)")

            Else
                Cmd.Parameters("@ProductId") = Me.PID.Text
                Cmd.ExecuteNonQuery("update Product set ProductCode=@ProductCode,ProductName=@ProductName,CategoryId= @CateId,Price=@Price,MinimumPrice=@MinimumPrice where ProductId = @ProductId")
            End If

        Me.DialogResult = Windows.Forms.DialogResult.OK
            FormProduct.LoadData()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub FormProductEditor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        PID.Hide()
        CboCateId.Hide()
        CbotyId.Hide()
        If PID.Text = "" Then
            cboCategory.Text = ""
            cboType.Text = ""
            Exit Sub
        Else
            Dim row = FormProduct.dgv.SelectedRows(0)

            cboCategory.Text = row.Cells("CategoryName").Value
            cboType.Text = row.Cells("TypeName").Value
        End If

    End Sub
    
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Dim dt As New DataTable
        dt = Cmd.ExecuteDataTable("select * from Category where Active=1")
        Me.cboCategory.DisplayMember = "CategoryName"
        Me.cboCategory.ValueMember = "CategoryId"
        Me.CboCateId.DisplayMember = "CategoryId"
   
        Me.cboCategory.DataSource = dt
        Me.CboCateId.DataSource = dt
        '   ---------------------------------------
        dt = Cmd.ExecuteDataTable("select * from Type where Active=1 ")
        Me.cboType.DisplayMember = "TypeName"
        Me.cboType.ValueMember = "TypeId"
        Me.CbotyId.DisplayMember = ("TypeId")
        Me.cboType.DataSource = dt
        Me.CbotyId.DataSource = dt
       

    End Sub

    Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub
 

    Private Sub cboType_Click(sender As Object, e As EventArgs) Handles cboType.Click
     

    End Sub
 
    Private Sub CbotyId_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CbotyId.SelectedIndexChanged
        Try


            Cmd.Parameters("@TypeId") = Me.CbotyId.Text
            cboCategory.DataSource = Cmd.ExecuteDataTable("select * from Category where TypeId= @TypeId")


        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class